from ast import literal_eval
from bs4 import BeautifulSoup

from .type import Track
from .get_html import get_html
from .utils import parse_json, remove_brackets


def parse_scripts(html):
    for script in html.select("script"):
        str_script = str(script)
        prefix = "window.__PRELOADED_STATE__ = JSON.parse("
        if prefix in str_script:
            for line in str_script.splitlines():
                if prefix not in line:
                    continue
                line = line.strip()
                line = line.removeprefix(prefix)
                line = line.removesuffix(");")
                json_string = literal_eval(line)
                parsed_track = parse_json(json_string)
                break
    song_page = parsed_track.songPage
    datas = {}
    for data in song_page.trackingData:
        value = data.value
        key = data.key
        datas[key] = value

    lyrics_html = BeautifulSoup(song_page.lyricsData.body.html, "html.parser")
    lyrics = lyrics_html.text.strip()

    return datas, lyrics


async def get_track(url) -> Track:
    html = await get_html(url)
    datas, lyrics = parse_scripts(html)
    cover_meta = html.select_one('meta[property="og:image"]')
    cover = None
    if cover_meta:
        cover = cover_meta["content"]

    return Track(
        artist=remove_brackets(datas["Primary Artist"]),
        title=remove_brackets(datas["Title"]),
        id=f"/songs/{datas['Song ID']}",
        url=url,
        cover=cover,
        lyrics=lyrics,
        release=datas["Release Date"],
        album=None,
        number=0
    )
